package lib.quasar.db.manager;

import android.util.Log;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;

import java.util.List;

/**
 * description: 当前类描述信息
 * created by kalu on 2018/4/16 20:54
 */
final class GreendaoManager {

    private static final String TAG = "GreendaoManager";

    /**
     * 模糊查询
     */
    public static <E> List<E> searchFuzzyData(AbstractDao dao, Property property, String key) {

        List<E> list = null;

        try {

            list = dao.queryBuilder().where(property.like("%" + key + "%")).list();
            Log.e(TAG, "searchFuzzyData ==> size = " + list.size());

        } catch (Exception e) {

            Log.e(TAG, "searchFuzzyData ==> " + e.getMessage(), e);
        } finally {

            return list;
        }
    }

    /**
     * 查询
     */
    public static <E> List<E> searchNormalData(AbstractDao dao, Property property, String key) {

        List<E> list = null;

        try {

            list = dao.queryBuilder().where(property.eq("%" + key + "%")).list();
            Log.e(TAG, "searchNormalData ==> size = " + list.size());

        } catch (Exception e) {

            Log.e(TAG, "searchFuzzyData ==> " + e.getMessage(), e);
        } finally {

            return list;
        }
    }

    /**
     * 查询
     */
    public static <E> E searchSingleData(AbstractDao dao, Property property, Object key) {

        E m = null;

        try {

            m = (E) dao.queryBuilder().where(property.eq(key)).list().get(0);
            Log.e(TAG, "searchNormalData ==> size = " + 1);

        } catch (Exception e) {

            Log.e(TAG, "searchFuzzyData ==> " + e.getMessage(), e);
        } finally {

            return m;
        }
    }

    /**
     * 普通查询
     */
    public static <E> List<E> searchAllData(AbstractDao dao) {

        List<E> list = null;

        try {

            list = dao.queryBuilder().list();
            Log.e(TAG, "searchAllData ==> size = " + list.size());

        } catch (Exception e) {

            Log.e(TAG, "searchAllData ==> " + e.getMessage(), e);
        } finally {

            return list;
        }
    }

    /**
     * 删除全部数据
     */
    public static void deleteAllData(AbstractDao dao) {

        try {
            dao.deleteAll();
            Log.e(TAG, "deleteAllData ==> result = " + true);
        } catch (Exception e) {
            Log.e(TAG, "deleteAllData ==> " + e.getMessage(), e);
        }
    }

    /**
     * 删除单个
     */
    public static void deleteData(AbstractDao dao, Long key) {

        try {
            dao.deleteByKey(key);
            Log.e(TAG, "deleteData ==> result = " + true);

        } catch (Exception e) {

            Log.e(TAG, "deleteData ==> " + e.getMessage(), e);
        }
    }

    /**
     * 插入单个
     */
    public static <E> void insertData(AbstractDao dao, E m) {

        try {
            dao.insert(m);
            Log.e(TAG, "insertData ==> result = " + true);

        } catch (Exception e) {

            Log.e(TAG, "insertData ==> " + e.getMessage(), e);
        }
    }

    /**
     * 修改
     */
    public static <T> void modifyData(AbstractDao dao, T entity) {

        try {

            dao.update(entity);
            Log.e(TAG, "modifyData ==> result = " + true);

        } catch (Exception e) {

            Log.e(TAG, "modifyData ==> " + e.getMessage(), e);
        }
    }
}
